package com.dbh.company.buildbook.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;

public class WhiteScreenDetector {
    
    // 采样检测，提高大图检测性能
    public static boolean isWhiteScreenWithSampling(BufferedImage image, double threshold, int sampleStep) {
        int width = image.getWidth();
        int height = image.getHeight();
        long whitePixels = 0;
        long sampledPixels = 0;
        
        for (int y = 0; y < height; y += sampleStep) {
            for (int x = 0; x < width; x += sampleStep) {
                int rgb = image.getRGB(x, y);
                int r = (rgb >> 16) & 0xFF;
                int g = (rgb >> 8) & 0xFF;
                int b = rgb & 0xFF;
                
                if (r > 240 && g > 240 && b > 240) {
                    whitePixels++;
                }
                sampledPixels++;
            }
        }
        
        double whiteRatio = (double) whitePixels / sampledPixels;
        return whiteRatio >= threshold;
    }
    
    public static void main(String[] args) throws Exception {
        String imageUrl = "https://img.ljcdn.com/element-library/prod-77ce43d9-edb7-45df-b7d1-1705ff8ce24d.png";
        SSLUtil.disableCertificateValidation();
        BufferedImage image = ImageIO.read(new URL(imageUrl));
        double threshold = 0.7; // 70%阈值
        int sampleStep = 5; // 采样步长，值越大检测越快但精度越低
        
        boolean isWhite = isWhiteScreenWithSampling(image, threshold, sampleStep);
        System.out.println("Is white screen (sampled): " + isWhite);
    }
}